import Vue from 'vue'
import { ACCESS_TOKEN, USER_NAME,USER_INFO } from "@/store/mutation-types"
import { login, logout, getInfo } from '@/api/user'
import {  removeToken } from '@/utils/auth'
import { welcome } from "@/utils/util"
import { resetRouter } from '@/router'

const state = {
  token: '',
  username: '',
  realname: '',
  welcome: '',
  avatar: '',
  permissionList: [],
  info: {}
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, { username, realname, welcome }) => {
    state.username = username
    state.realname = realname
    state.welcome = welcome
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PERMISSIONLIST: (state, permissionList) => {
    state.permissionList = permissionList
  },
  SET_INFO: (state, info) => {
    state.info = info
  },
}

const actions = {
  // user login
  Login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        if(response.success){
          const { token,userInfo } = response.data
          Vue.ls.set(ACCESS_TOKEN, token,   24 * 60 * 60 * 1000)
          Vue.ls.set(USER_NAME, userInfo.username, 24 * 60 * 60 * 1000)
          Vue.ls.set(USER_INFO, userInfo, 24 * 60 * 60 * 1000)

          commit('SET_TOKEN', token)
          commit('SET_INFO', userInfo)
          commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname, welcome: welcome() })
          commit('SET_AVATAR', userInfo.avatar)
          resolve(response)
        }else{
          reject(response)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  GetPermissionList({ commit, state }) {
    return new Promise((resolve, reject) => {
      resolve()
      // queryPermissionsByUser(state.token).then(response => {
      //   const { data } = response
      //
      //   if (!data) {
      //     reject('Verification failed, please Login again.')
      //   }
      //
      //   const { name, avatar } = data
      //
      //   commit('SET_NAME', name)
      //   commit('SET_AVATAR', avatar)
      //   resolve(data)
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // user logout
  Logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      let logoutToken = state.token;
      commit('SET_TOKEN', '')
      commit('SET_PERMISSIONLIST', [])
      Vue.ls.remove(ACCESS_TOKEN)
      //console.log('logoutToken: '+ logoutToken)
      logout(logoutToken).then(() => {
        //var sevice = "http://"+window.location.host+"/";
        //var serviceUrl = encodeURIComponent(sevice);
        //window.location.href = window._CONFIG['casPrefixUrl']+"/logout?service="+serviceUrl;
        resolve()
      }).catch(() => {
        resolve()
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  state,
  mutations,
  actions
}

